﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MTQ.Common
{
    public class HtmlCodeHelper
    {
        private string _strHtml { get; set; }

        public HtmlCodeHelper()
        {

        }
        public HtmlCodeHelper(string strHtml)
        {
            _strHtml = strHtml;
        }

        public static string[] GetOutHtml(string str, string tag)
        {

            string pattern = @"<" + tag + "(.*?)>(.*?)(</" + tag + ">|<" + tag + "(.*?)>)";
            Regex regex = new Regex(pattern);
            MatchCollection matches = regex.Matches(str);

            MatchCollection mc = Regex.Matches(str, pattern);
            List<string> list = new List<string>();

            foreach (Match item in matches)
            {
                list.Add(item.Groups[0].Value);
            }
            return list.ToArray();
        }
        public static string[] GetHtml(string str, string tag)
        {

            string pattern = @"<" + tag + "(.*?)>(.*?)</" + tag + ">";
            Regex regex = new Regex(pattern);
            MatchCollection matches = regex.Matches(str);

            MatchCollection mc = Regex.Matches(str, pattern);
            List<string> list = new List<string>();

            foreach (Match item in matches)
            {
                list.Add(item.Groups[item.Groups.Count - 1].Value);
            }
            return list.ToArray();
        }

        public static DataSet GetDataSet(string strHtml)
        {
            DataSet ds = new DataSet();
            string[] strTables = GetOutHtml(strHtml, "table");
            foreach (string strTable in strTables)
            {
                DataTable dt = GetDataTable(strTable);
                ds.Tables.Add(dt);
            }
            return ds;

        }
        public static DataTable GetDataTable(string strHtml, bool notFirstRow = false)
        {
            DataTable dt = new DataTable();

            string[] strTrs = GetOutHtml(strHtml, "tr");

            Regex regex = new Regex("(<(.*?)>)|(,)", RegexOptions.IgnoreCase);
            for (int i = 0; i < strTrs.Count(); i++)
            {

                string[] tds = GetOutHtml(strTrs[i], "td");

                //构建table的列
                if (i == 0)
                {
                    for (int j = 0; j < tds.Count(); j++)
                    {
                        dt.Columns.Add("C_" + j, typeof(string));
                    }
                    if (notFirstRow)
                    {
                        for (int j = 0; j < tds.Count(); j++)
                        {
                            dt.Columns[j].ColumnName = regex.Replace(tds[j].ToString(), "");
                        }
                        continue;
                    }
                }
                DataRow dr = dt.NewRow();
                for (int j = 0; j < tds.Count(); j++)
                {

                    dr[j] = regex.Replace(tds[j].ToString(), "");
                }
                dt.Rows.Add(dr);
            }




            return dt;

        }


    }

}
